package data.model;

/**
 * Contains processor-specific information for an action.
 * Delay distributions, failure probabilities and time taken.
 * 
 * @author Will Home
 *
 */

public class ProcAction implements Comparable<ProcAction>{

        /** reference to actID of action */
        private int actRef;

        /** Lower and upper bounds of delay */
        private int lowBound, upBound;
        
        /** Probability of denominator */
        private int prob;
        
        /** Probability of failure */
        private int failNum, failDenom;

        /** check for delay and failure */
        private boolean delay, faulty;
        
        /**
         * 
         * @param actionRef
         */
        public ProcAction(int actionRef)
        {
                actRef = actionRef;
        }
        
        /**
         * Set boundaries of uniform delay distribution
         * 
         * @param up - upper bound
         * @param low - lower bound
         */
        public void SetDelay(int up, int low)
        {
                if( up > low)
                {
                        upBound = up;
                        lowBound = low;
                        prob = 1 + up - low;
                }
                else if( up < low)
                {
                        upBound = low;
                        lowBound = up;
                        prob = 1 + low - up;
                }
                else
                {
                        lowBound = low;
                        prob = 1 + up - low;
                }
                
               if(up == low)
            	   delay = false;
               else
            	   delay = true;
        }
        
        /**
         * Set probability of failure
         * 
         * @param num - Numerator
         * @param denom - Denominator
         * @return success check
         */
        public boolean SetFaulty(int num, int denom)
        {
        	if( num < denom)
        	{
        		failNum = num;
        		failDenom = denom;
        		faulty = true;
        		return true;
        	}
        	else if( num == denom)
        	{
        		faulty = false;
        		return true;
        	}
        	else
        		return false;
        }

        /**
         * get probability of failure as a rational
         * 
         * @return p(Fail)
         */
        public int[] getFaulty()
        {
                int[] p = {failNum, failDenom};
                return p;
        }
        
        /**
         * Get delay distribution
         * 
         * @return lower bound, upper bound, p(Success)
         */
        public int[] getDelay()
        {
                int[] p = {lowBound, upBound, prob};
                return p;
        }
        
        /**
         *  
         * @return action ID
         */
        public int getAction()
        {
                return actRef;
        }
        
        /**
         * 
         * @return true if there is a delay
         */
        public boolean isDelay()
        {
        	return delay;
        }
        
        /**
         * 
         * @return true if there is a fault
         */
        public boolean isFaulty()
        {
        	return faulty;
        }

        
        @Override
        public int compareTo(ProcAction other) {
                if(this.actRef == other.getAction())
                        return 0;
                else if(this.actRef < other.getAction())
                        return -1 ;
                else
                        return 1;
        }
        
}

